package com.lntu.meeting.controller;

import com.alibaba.fastjson.JSON;
import com.lntu.meeting.pojo.Notes;
import com.lntu.meeting.service.DeleteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class DeleteController {
    @Autowired
    private DeleteService deleteService;

    @RequestMapping("/deleteOne")
    public String delete(@RequestBody String id1) {
        if (id1 != null) {
            String s = id1.substring(0, id1.length() - 1);

            Integer id = Integer.valueOf(s);

            Map<String, Object> resMap = new HashMap();
        }


        if (id != null) {
            Integer num = deleteService.deleteByiId(id);
            if (num != 1) {
                System.out.println("删除失败");
                resMap.put("flag", -1);
                return JSON.toJSONString(resMap);

            }
            System.out.println("删除成功");
            resMap.put("flag", 1);
            String res = JSON.toJSONString(resMap);
            return res;
        }
        resMap.put("flag", -1);
        return JSON.toJSONString(resMap);
    }

    //byid
    public String selectMyList(@RequestBody String id1) {
        String flag = "500";
        Map<String, Object> resMap = new HashMap();
        if (id1 != null) {
            String loginId = id1.substring(0, id1.length() - 1);
            Integer id = Integer.valueOf(loginId);
            Calendar currentDate = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String d = simpleDateFormat.format(currentDate.getTime());
            Date cdate = null;
            try {
                cdate = simpleDateFormat.parse(d);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            Date date = cdate;
            List<Notes> notes = deleteService.queryId(id, date);
            if (notes.size() > 0) {
                System.out.println("查询成功");
                flag ="200";
                resMap.put("flag",flag);
                resMap.put("data",notes);
                String res = JSON.toJSONString(resMap);
                return res;
            }
        }
        return JSON.toJSONString(resMap);

    }
}
